package android.lollapalooza;

import java.util.List;

import org.jsoup.Jsoup;

import service.LollaService;
import service.LollaServiceImpl;
import util.ActivityUtil;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import entities.Band;
import entities.Event;

public class ArtistView extends Activity {

	private LollaService mService;
	private TextView mName;
	private TextView mWhereWhen;
	private TextView mDescription;
	private TextView mSetlist;
	private ImageView mImage;
	private Button mWebsite;
	private Button mYouTube;
	private Button mTwitter;
	private Button mMySpace;

	private String mWebsiteUrl;
	private String mYouTubeId;
	private String mTwitterId;
	private String mMySpaceUrl;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.artistview);
		mService = new LollaServiceImpl(getApplicationContext());

		mName = (TextView) findViewById(R.id.name);
		mWhereWhen = (TextView) findViewById(R.id.wherewhen);
		mSetlist = (TextView) findViewById(R.id.setlist);
		mDescription = (TextView) findViewById(R.id.description);
		mImage = (ImageView) findViewById(R.id.bandimg);
		mWebsite = (Button) findViewById(R.id.website);
		mYouTube = (Button) findViewById(R.id.youtube);
		mTwitter = (Button) findViewById(R.id.twitter);
		mMySpace = (Button) findViewById(R.id.MySpace);

		Bundle extras = getIntent().getExtras();
		String name = extras.getString("NAME");
		mName.setText(name);

		Band band = (Band) mService.getByName(name, Band.class);
		band.getSetlist();
		String description = band.getDescription();
		if (description != null) {
			String parsedDescription = Jsoup.parse(description).text();
			mDescription.setText(parsedDescription);
		}
		
		String imgUrl = band.getPhoto();
		if (imgUrl != null) {
			if (ActivityUtil.setImageViewFromUrl(mImage, imgUrl))
				mImage.setVisibility(View.VISIBLE);
		}

		String setlist = band.getSetlist();
		if (setlist != null) {
			setlist = setlist.replace("&nbsp;", "\n").replace("<br>", "\n")
					.replace("<br/>", "\n").replace("<br />", "\n").replace(
							"<div>", "").replace("</div>", "").replace("&amp;", " & ");
			mSetlist.setText("Setlist\n\n");
			mSetlist.append(setlist);
		}

		List<Event> events = mService.getEventsByBand(band);
		StringBuilder sb = new StringBuilder();
		for (Event event : events)
			sb.append(mService.getVenueByEvent(event).getTitle() + " Stage\n"
					+ event.getStartTime() + "\n\n");
		String whereWhen = "";
		if (events.size() > 0)
		    whereWhen = sb.toString().substring(0, sb.toString().length() - 2);
		long votes = band.getFanCount();
		if (votes > 0)
			whereWhen = whereWhen.concat("\n" + votes + " Lolla fans");
		mWhereWhen.setText(whereWhen);

		mWebsiteUrl = band.getLink();
		if (mWebsiteUrl != null) {
			mWebsite.setVisibility(View.VISIBLE);
			mWebsite.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					launchWebActivity(mWebsiteUrl);
				}
			});
		}

		mYouTubeId = band.getYoutubeId();
		if (mYouTubeId != null) {
			mYouTube.setVisibility(View.VISIBLE);
			mYouTube.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					launchWebActivity("http://www.youtube.com/watch?v="
							+ mYouTubeId);
				}
			});
		}

		mTwitterId = band.getTwitterUsername();
		if (mTwitterId != null) {
			mTwitter.setVisibility(View.VISIBLE);
			mTwitter.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					launchWebActivity("http://www.twitter.com/" + mTwitterId);
				}
			});
		}

		mMySpaceUrl = band.getMyspaceLink();
		if (mMySpace != null) {
			mMySpace.setVisibility(View.VISIBLE);
			mMySpace.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					launchWebActivity(mMySpaceUrl);
				}
			});
		}
	}

	private void launchWebActivity(String url) {
		final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
				.parse(url));
		startActivity(intent);
	}

}
